package Test;

import junit.framework.TestCase;

import CtrLayer.ServiceCtr;
import DataLayer.Model.Service;

public class testServiceCtr extends TestCase
{
	private ServiceCtr sCtr;
	private Service service;
	
	public void setUp() throws Exception
	{
		sCtr = new ServiceCtr();
		service = new Service("40K test", "Star tank", "Champion", 350, true);
	}

	public void tearDown() throws Exception
	{
	}
	
	public void testServiceController()
	{
		try
		{
			createService();
			Service dbServ = getService(service.getSpil());
			assertEquals(service.getModel(), dbServ.getModel());
			assertEquals(service.getMalestandard(), dbServ.getMalestandard());
			assertEquals(service.getPris(), dbServ.getPris());
			assertEquals(service.isSynlig(), dbServ.isSynlig());
			int rc = deleteService(dbServ);
			assertEquals(1, rc);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void createService() throws Exception
	{
		sCtr.createService(
				service.getSpil(), service.getModel(), service.getMalestandard(), service.getPris(),
				service.isSynlig());
		System.out.println("#1 Servicen " + service.getModel() + " blev oprettet");
	}

	private Service getService(String spil)
	{
		Service tmpServ = null;
		for (Service s : sCtr.getAllServicer(service.isSynlig()))
		{
			if (s.getSpil().equals(spil))
			{
				tmpServ = s;
				System.out.println("#2 Servicen " + s.getModel() + " blev fundet");
			}
		}
		return tmpServ;
	}

	private int deleteService(Service s)
	{
		int rowcount = -1;
		rowcount = sCtr.deleteService(s);
		System.out.println("#3 Servicen " + s.getModel() + " blev slettet");
		return rowcount;
	}
}
